home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / stdlib / RCS / stdlib.c,v < prev   
Encoding:
Text File  |  1988-06-17  |  10.0 KB  |  548 lines

  1. head     1.2;
  2. access   ;
  3. symbols  ;
  4. locks    ; strict;
  5. comment  @ * @;
  6.  
  7.  
  8. 1.2
  9. date     88.06.08.13.28.53;  author ouster;  state Exp;
  10. branches ;
  11. next     1.1;
  12.  
  13. 1.1
  14. date     88.05.20.16.34.06;  author ouster;  state Exp;
  15. branches ;
  16. next     ;
  17.  
  18.  
  19. desc
  20. @@
  21.  
  22.  
  23. 1.2
  24. log
  25. @*** empty log message ***
  26. @
  27. text
  28. @/* 
  29.  * stdlib.c --
  30.  *
  31.  *    This file contains a program that exercises the stdlib
  32.  *    library facilities.  Invoke it with no parameters;  it
  33.  *    will print messages on stderr for any problems it detects
  34.  *    with the string procedures.
  35.  *
  36.  * Copyright 1988 Regents of the University of California
  37.  * Permission to use, copy, modify, and distribute this
  38.  * software and its documentation for any purpose and without
  39.  * fee is hereby granted, provided that the above copyright
  40.  * notice appear in all copies.  The University of California
  41.  * makes no representations about the suitability of this
  42.  * software for any purpose.  It is provided "as is" without
  43.  * express or implied warranty.
  44.  */
  45.  
  46. #ifndef lint
  47. static char rcsid[] = "$Header: stdlib.c,v 1.1 88/05/20 16:34:06 ouster Exp $ SPRITE (Berkeley)";
  48. #endif not lint
  49.  
  50. #include <sprite.h>
  51. #include <proc.h>
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <sys/wait.h>
  56. #include <signal.h>
  57.  
  58. #define error(string) \
  59.     fprintf(stderr, string); \
  60.     exit(1);
  61.  
  62. int
  63. sortFunc(a, b)
  64.     char **a, **b;
  65. {
  66.     return strcmp(*a, *b);
  67. }
  68.  
  69. int
  70. checkDifferent(ptr, count)
  71.     int *ptr;            /* Pointer to array of integers to check
  72.                  * to see if they're all different. */
  73.     int count;            /* How many integers in array. */
  74. {
  75.     int i, j;
  76.  
  77.     for (i = 0; i < count-1; i++) {
  78.     for (j = i+1; j < count; j++) {
  79.         if (ptr[i] == ptr[j]) {
  80.         return 1;
  81.         }
  82.     }
  83.     }
  84.     return 0;
  85. }
  86.  
  87. int exitValue = 22;
  88.  
  89. void
  90. exitFunc1()
  91. {
  92.     _exit(exitValue);
  93. }
  94.  
  95. void
  96. exitFunc2()
  97. {
  98.     exitValue = 14;
  99. }
  100.  
  101. main()
  102. {
  103.     int result = 0, i;
  104.     long int l, m, n;
  105.     char *term, *arg, *p;
  106.     double d;
  107.     div_t dt;
  108.     ldiv_t ldt;
  109.     int nums[20], nums2[20];
  110.     static char *strings[] = {
  111.     "beta",
  112.     "alpha",
  113.     "gamma",
  114.     "delta",
  115.     "charlie",
  116.     "smoke",
  117.     "aleph"
  118.     };
  119.     union wait status;
  120.     int pid, savedStdout, pipes[2];
  121.     char line[100];
  122.     static struct sigvec vec;
  123.  
  124.     /*
  125.      * atof
  126.      */
  127.  
  128.     if (atof("1a") != 1.0) {
  129.     error("atof error 1\n");
  130.     }
  131.     if (atof(".123,") != .123) {
  132.     error("atof error 2\n");
  133.     }
  134.     if (atof("-47.e2") != -4700.0) {
  135.     error("atof error 3\n");
  136.     }
  137.     if (atof("+0.2E-2") != .002) {
  138.     error("atof error 4\n");
  139.     }
  140.     if (atof("-123456789.01234") != -123456789.01234) {
  141.     error("atof error 5\n");
  142.     }
  143.     if (atof("a") != 0.0) {
  144.     error("atof error 6\n");
  145.     }
  146.     d = atof("e");
  147.     if (d != 0.0) {
  148.     error("atof error 6\n");
  149.     }
  150.     if (atof("-e5") != 0.0) {
  151.     error("atof error 7\n");
  152.     }
  153.  
  154.     /*
  155.      * atoi
  156.      */
  157.  
  158.     if (atoi("") != 0) {
  159.     error("atoi error 1\n");
  160.     }
  161.     if (atoi("47") != 47) {
  162.     error("atoi error 2\n");
  163.     }
  164.     if (atoi("-42a") != -42) {
  165.     error("atoi error 3\n");
  166.     }
  167.     if (atoi("a") != 0) {
  168.     error("atoi error 4\n");
  169.     }
  170.     if (atoi("09/6") != 9) {
  171.     error("atoi error 5\n");
  172.     }
  173.     if (atoi("55:") != 55) {
  174.     error("atoi error 6\n");
  175.     }
  176.     if (atoi("+189223") != 189223) {
  177.     error("atoi error 7\n");
  178.     }
  179.  
  180.     /*
  181.      * atol
  182.      */
  183.  
  184.     if (atol("") != 0) {
  185.     error("atol error 1\n");
  186.     }
  187.     if (atol("47") != 47) {
  188.     error("atol error 2\n");
  189.     }
  190.     if (atol("-42a") != -42) {
  191.     error("atol error 3\n");
  192.     }
  193.     if (atol("a") != 0) {
  194.     error("atol error 4\n");
  195.     }
  196.     if (atol("09/6") != 9) {
  197.     error("atol error 5\n");
  198.     }
  199.     if (atol("55:") != 55) {
  200.     error("atol error 6\n");
  201.     }
  202.     if (atol("+189223") != 189223) {
  203.     error("atol error 7\n");
  204.     }
  205.  
  206.     /*
  207.      * strtod
  208.      */
  209.  
  210.     arg = "1a";
  211.     if (strtod(arg, &term) != 1.0) {
  212.     error("strtod error 1\n");
  213.     }
  214.     if (term != (arg+1)) {
  215.     error("strtod error 2\n");
  216.     }
  217.     arg = ".123,";
  218.     if (strtod(arg, &term) != .123) {
  219.     error("strtod error 3\n");
  220.     }
  221.     if (term != (arg+4)) {
  222.     error("strtod error 4\n");
  223.     }
  224.     arg = "-47.e2";
  225.     if (strtod(arg, &term) != -4700.0) {
  226.     error("strtod error 5\n");
  227.     }
  228.     if (term != (arg+6)) {
  229.     error("strtod error 6\n");
  230.     }
  231.     if (strtod("+0.2E-2", (char *) NULL) != .002) {
  232.     error("strtod error 7\n");
  233.     }
  234.     if (strtod("-123456789.01234", (char *) NULL) != -123456789.01234) {
  235.     error("strtod error 8\n");
  236.     }
  237.     arg = "a";
  238.     if (strtod(arg, &term) != 0.0) {
  239.     error("strtod error 9\n");
  240.     }
  241.     if (term != arg) {
  242.     error("strtod error 10\n");
  243.     }
  244.     if (strtod("e", (char *) NULL) != 0.0) {
  245.     error("strtod error 11\n");
  246.     }
  247.     arg = "-e5";
  248.     if (strtod(arg, &term) != 0.0) {
  249.     error("strtod error 12\n");
  250.     }
  251.     if (term != arg) {
  252.     error("strtod error 13\n");
  253.     }
  254.  
  255.     /*
  256.      * strtol
  257.      */
  258.  
  259.     arg = "442x";
  260.     if (strtol(arg, &term, 10) != 442) {
  261.     error("strtol error 1\n");
  262.     }
  263.     if (term != (arg+3)) {
  264.     error("strtol error 2\n");
  265.     }
  266.     arg = "+01009";
  267.     if (strtol(arg, &term, 0) != 0100) {
  268.     error("strtol error 3\n");
  269.     }
  270.     if (term != (arg+5)) {
  271.     error("strtol error 4\n");
  272.     }
  273.     arg = "0x1abcDefgh";
  274.     if (strtol(arg, &term, 0) != 0x1abcdef) {
  275.     error("strtol error 5\n");
  276.     }
  277.     if (term != (arg+9)) {
  278.     error("strtol error 6\n");
  279.     }
  280.     arg = "18";
  281.     if (strtol(arg, &term, 0) != 18) {
  282.     error("strtol error 7\n");
  283.     }
  284.     if (term != (arg+2)) {
  285.     error("strtol error 8\n");
  286.     }
  287.     if (strtol("-0x10", (char *) NULL, 16) != -16) {
  288.     error("strtol error 9\n");
  289.     }
  290.     if (strtol("j0k", (char *) NULL, 20) != 380) {
  291.     error("strtol error 10\n");
  292.     }
  293.     arg = "0xq";
  294.     if (strtol(arg, &term, 0) != 0) {
  295.     error("strtol error 11\n");
  296.     }
  297.     if (term != arg) {
  298.     error("strtol error 12\n");
  299.     }
  300.  
  301.     /*
  302.      * strtoul
  303.      */
  304.  
  305.     arg = "12345";
  306.     if (strtoul(arg, &term, 0) != 12345) {
  307.     error("strtoul error 1\n");
  308.     }
  309.     if (term != &arg[5]) {
  310.     error("strtoul error 2\n");
  311.     }
  312.     arg = "-22";
  313.     if (strtoul(arg, &term, 0) != 0) {
  314.     error("strtoul error 3\n");
  315.     }
  316.     if (term != arg) {
  317.     error("strtoul error 4\n");
  318.     }
  319.     arg = "+14";
  320.     if (strtoul(arg, &term, 0) != 0) {
  321.     error("strtoul error 5\n");
  322.     }
  323.     if (term != arg) {
  324.     error("strtoul error 6\n");
  325.     }
  326.  
  327.     /*
  328.      * abs
  329.      */
  330.  
  331.     if (abs(-1) != 1) {
  332.     error("abs error 1\n");
  333.     }
  334.     if (abs(22) != 22) {
  335.     error("abs error 2\n");
  336.     }
  337.     if (abs(0) != 0) {
  338.     error ("abs error 3\n");
  339.     }
  340.  
  341.     /*
  342.      * labs
  343.      */
  344.  
  345.     l = labs((long int) -1);
  346.     if (l != 1) {
  347.     error("labs error 1\n");
  348.     }
  349.     l = labs((long int) 22);
  350.     if (l != 22) {
  351.     error("labs error 2\n");
  352.     }
  353.     l = labs((long int) 0);
  354.     if (l != 0) {
  355.     error ("labs error 3\n");
  356.     }
  357.  
  358.     /*
  359.      * div
  360.      */
  361.  
  362.     dt = div(22, 3);
  363.     if ((dt.quot != 7) || (dt.rem != 1)) {
  364.     error("div error 1\n");
  365.     }
  366.     dt = div(-22, 3);
  367.     if ((dt.quot != -7) || (dt.rem != -1)) {
  368.     error("div error 2\n");
  369.     }
  370.     dt = div(22, -3);
  371.     if ((dt.quot != -7) || (dt.rem != 1)) {
  372.     error("div error 3\n");
  373.     }
  374.     dt = div(-22, -3);
  375.     if ((dt.quot != 7) || (dt.rem != -1)) {
  376.     error("div error 4\n");
  377.     }
  378.  
  379.     /*
  380.      * ldiv
  381.      */
  382.  
  383.     ldt = ldiv((long int) 22, (long int) 3);
  384.     if ((ldt.quot != (long int) 7) || (ldt.rem != (long int) 1)) {
  385.     error("ldiv error 1\n");
  386.     }
  387.     ldt = ldiv((long int) -22, (long int) 3);
  388.     if ((ldt.quot != (long int) -7) || (ldt.rem != (long int) -1)) {
  389.     error("ldiv error 2\n");
  390.     }
  391.     ldt = ldiv((long int) 22, (long int) -3);
  392.     if ((ldt.quot != (long int) -7) || (ldt.rem != (long int) 1)) {
  393.     error("ldiv error 3\n");
  394.     }
  395.     ldt = ldiv((long int) -22, (long int) -3);
  396.     if ((ldt.quot != (long int) 7) || (ldt.rem != (long int) -1)) {
  397.     error("ldiv error 4\n");
  398.     }
  399.  
  400.     /*
  401.      * qsort
  402.      */
  403.  
  404.     qsort((char *) strings, 7, sizeof(char *), sortFunc);
  405.     for (i = 0; i < 6; i++) {
  406.     if (strcmp(strings[i+1], strings[i]) < 0) {
  407.         error("qsort error 1\n");
  408.     }
  409.     }
  410.  
  411.     /*
  412.      * rand
  413.      */
  414.  
  415.     for (i = 0;  i < 20; i++) {
  416.     nums[i] = rand();
  417.     }
  418.     if (checkDifferent(nums, 20)) {
  419.     error("rand error 1\n");
  420.     }
  421.     srand(1);
  422.     for (i = 0; i < 20; i++) {
  423.     if (rand() != nums[i]) {
  424.         error("rand error 2\n");
  425.     }
  426.     }
  427.     srand(12345);
  428.     for (i = 0; i < 20; i++) {
  429.     nums2[i] = rand();
  430.     }
  431.     if (checkDifferent(nums2, 20)) {
  432.     error("rand error 3\n");
  433.     }
  434.     for (i = 0; i < 20; i++) {
  435.     if (nums2[i] == nums[i]) {
  436.         error("rand error 4\n");
  437.     }
  438.     }
  439.  
  440.     /*
  441.      * atexit
  442.      */
  443.  
  444.     if (fork() == 0) {
  445.     atexit(exitFunc1);
  446.     atexit(exitFunc2);
  447.     exit();
  448.     }
  449.     wait(&status);
  450.     if (status.w_T.w_Retcode != 14) {
  451.     error("atexit error 1\n");
  452.     }    
  453.  
  454.     /*
  455.      * setenv/getenv
  456.      */
  457.  
  458.     setenv("TEST1", "foo");
  459.     setenv("TEST2", "bar");
  460.     setenv("TEST1", "foo2");
  461.     setenv("TEST3", "helpMe");
  462.     p = getenv("TEST1");
  463.     if ((p == 0) || (strcmp(p, "foo2") != 0)) {
  464.     error("getenv error 1\n");
  465.     }
  466.     p = getenv("TEST2");
  467.     if ((p == 0) || (strcmp(p, "bar") != 0)) {
  468.     error("getenv error 2\n");
  469.     }
  470.     p = getenv("TEST3");
  471.     if ((p == 0) || (strcmp(p, "helpMe") != 0)) {
  472.     error("getenv error 3\n");
  473.     }
  474.     if (getenv("IdontEXIST") != 0) {
  475.     error("getenv error 4\n");
  476.     }
  477.  
  478.     /*
  479.      * abort
  480.      */
  481.  
  482.     pid = fork();
  483.     if (pid == 0) {
  484.     abort();
  485.     }
  486.     while (1) {
  487.     int pid2;
  488.     pid2 = wait3(&status, WUNTRACED, 0);
  489.     if (pid2 == -1) {
  490.         error("abort error 1\n");
  491.     }
  492.     if (status.w_status == 0) {
  493.         continue;
  494.     }
  495.     if (pid2 == pid) {
  496.         break;
  497.     }
  498.     }
  499.     kill(pid, SIGKILL);
  500.     if ((status.w_S.w_Stopval != WSTOPPED)
  501.         || (status.w_S.w_Stopsig != SIGQUIT)) {
  502.     error("abort error 2\n");
  503.     }
  504.  
  505.     /*
  506.      * system
  507.      */
  508.  
  509.     pipe(pipes);
  510.     savedStdout = dup(1);
  511.     dup2(pipes[1], 1);
  512.     system("echo This is just a test");
  513.     dup2(savedStdout, 1);
  514.     i = read(pipes[0], line, 99);
  515.     if (i < 0) {
  516.     error("system error 1\n");
  517.     }
  518.     line[i] = 0;
  519.     if (strcmp(line, "This is just a test\n") != 0) {
  520.     error("system error 2\n");
  521.     }
  522.     close(pipes[0]);
  523.     close(pipes[1]);
  524.  
  525.     return result;
  526. }
  527. @
  528.  
  529.  
  530. 1.1
  531. log
  532. @Initial revision
  533. @
  534. text
  535. @d20 1
  536. a20 1
  537. static char rcsid[] = "$Header: string.c,v 1.1 88/04/25 13:09:17 ouster Exp $ SPRITE (Berkeley)";
  538. d23 2
  539. d27 3
  540. d35 39
  541. d76 3
  542. a78 2
  543.     int result = 0;
  544.     char *term, *arg;
  545. d80 16
  546. d273 224
  547. @
  548.